package hu.szyrt.bug.connections;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import hu.szyrt.bug.models.Comment;
import hu.szyrt.bug.utils.Config;

public class CommentLoadConnection extends BaseConnection {

	private List<Comment> comments;
	
	public CommentLoadConnection() {
		super(Config.URL_BUG_COMMENT_LOAD);
	}
	
	
	@Override
	public void run() {
		parameterizedURL();		
		if(download()){
			// JSON decode
			try {
				//JSONObject jsonResult = new JSONObject(rawResults);
				JSONArray jsonResult = new JSONArray(rawResults);
				this.comments = new ArrayList<Comment>();
				for(int i=0; i<jsonResult.length(); i++){
					JSONObject jsonObject = jsonResult.getJSONObject(i);
					this.comments.add(new Comment(
							jsonObject.getString("name"),
							jsonObject.getString("date"),
							jsonObject.getString("comment")
							));
				}
				postSuccess();							
			} catch (JSONException e) {
				e.printStackTrace();
				postError();
			}
		}else{
			postError();
		}		
	}
	
	
	// Setters ------------------
	public void setPlaceID(int placeID){
		params.put("place_id", String.valueOf(placeID));
	}
	
	// Getters ------------------
	public int getPlaceID(){
		return Integer.parseInt(params.get("place_id"));
	}
	
	public List<Comment> getComments() {
		return comments;
	}

}
